import React, { useEffect, useState, useContext } from "react";
import { Modal, Form, Input, Button } from "antd";
import { connect } from 'dva';
import { sampleContext } from '../index';
import { changeSample } from '@/service/sample'


const theProps = (state) => {
    return {
    }
}

function EditModal(props) {
    const [form] = Form.useForm()
    const editInfo = useContext(sampleContext);
    const [initInput, setInitInput] = useState({});
    form.setFieldsValue(initInput)
    useEffect(() => {
        form.resetFields();
        let sample = editInfo?.sample?.sample
        let arr = []
        for (let i of sample) {
            if (i?.id === editInfo.id) {
                arr = i
                break
            }
        }
        setInitInput(arr)
    }, [editInfo, form]);
    const { dispatch } = props;
    const onFinish = (value) => {
        changeSample({
            id: editInfo.id,
            region: value.region,
            wellBlock: value.wellBlock,
            wellDeep: value.wellDeep,
            wellLayer: value.wellLayer,
            wellNum: value.wellNum,
            info: value.info,
            name: value.name
        })
    }
    console.log(initInput)
    return (
        <Modal
            title="编辑"
            footer={[
            ]}
            onCancel={() => { editInfo.setVisible(false) }}
            visible={editInfo.visible}>
            <Form
                name="basic"
                // labelCol={{
                //     span: 8,
                // }}
                // wrapperCol={{
                //     span: 16,
                // }}
                form={form}
                initialValues={{
                    remember: true,
                }}
                onFinish={onFinish}
                // onFinishFailed={onFinishFailed}
                autoComplete="off"
                
            >
                <Form.Item
                    name="name"
                    label="样品名称"
                    initialValue={initInput.name}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    name="region"
                    label="区块"
                    initialValue={initInput.region}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    name="wellBlock"
                    label="井区"
                    initialValue={initInput.wellBlock}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>



                <Form.Item
                    name="wellDeep"
                    label="井深"
                    initialValue={initInput.wellDeep}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    name="wellLayer"
                    label="层位"
                    initialValue={initInput.wellLayer}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    name="wellNum"
                    label="井号"
                    initialValue={initInput.wellNum}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="info"
                    label="样品性质"
                    initialValue={initInput.info}
                    rules={[
                        {
                            required: true,
                            message: '该字段不能为空',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item style={{ textAlign: 'right' }}>
                    <Button
                        type="primary" htmlType="submit">
                        确认
                    </Button>
                </Form.Item>
            </Form>
        </Modal>
    )
}
export default connect(theProps, null)(EditModal)